//
//  AppDelegate.m
//  RBDemoProj
//
//  Created by Lan on 2018/2/2.
//  Copyright © 2018年 SummerTea. All rights reserved.
//

#import "AppDelegate.h"
#import "LCAppDelegate.h"

#import "NSString+RBAdd.h"
#import "RBRequestHM.h"
#import "RBAccountManager.h"
#import <AFNetworking.h>
#import "RanTool.h"
#import "RBPersistence.h"
#import "RBEncryption.h"
#import "RBAlipayUtils.h"
#import "RBWechatUtils.h"

@interface AppDelegate ()

@property(nonatomic, strong) RBRequestHM *hm;

@property(nonatomic, strong) UIApplication *application;
@property(nonatomic, strong) NSDictionary *launchOptions;

@end

@implementation AppDelegate

//程序启动成功之后会自动调用的方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //扩展方法
    [[LCAppDelegate share] configAppDelegate:launchOptions];
    //创建windows
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    __weak typeof(self) weak_self = self;
    //国行手机网络授权
    if (![RBAccountManager netPass]) {
        self.application = application;
        self.launchOptions = launchOptions;
        
        [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
                [[AFNetworkReachabilityManager sharedManager] stopMonitoring];
                [RBAccountManager setNetPass:YES];
                
                [weak_self checkVersion];
                [weak_self vendorUtilsSetupWithOptions:weak_self.launchOptions application:weak_self.application];
            }
        }];
        [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    } else {
        //三方配置
        [self vendorUtilsSetupWithOptions:launchOptions application:application];
        [self checkVersion];
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            UIViewController *vc = [NSClassFromString(@"RBPagerDemo2VC") new];
//            self.window.rootViewController = vc;
    });
    
    return YES;
}

//应用程序将要变成非活跃状态的时候会自动调用的方法
- (void)applicationWillResignActive:(UIApplication *)application {
    
}

//应用程序已经进入后台的时候会调用的方法
- (void)applicationDidEnterBackground:(UIApplication *)application {
}

//应用程序将要进入前台会调用的方法
- (void)applicationWillEnterForeground:(UIApplication *)application {
}

//应用程序已经变成活跃状态的时候会调用的方法
- (void)applicationDidBecomeActive:(UIApplication *)application {
}

//应用程序将要终止的时候会调用的方法
- (void)applicationWillTerminate:(UIApplication *)application {
}

//打开外部APP
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    NSLog(@"系统的方法");
    if ([url.absoluteString hasPrefix:@"gotoPage"]) {
        NSLog(@"%@", url.absoluteString.lastPathComponent);
    }
    
    [RBAlipayUtils handleUrl:url];
    return [RBWechatUtils handleUrl:url];
}

#pragma mark 检查版本
- (void)checkVersion {
//    [RanTool updateTTC];
//    [RanTool updateVersionWithHudSuperView:nil];
}

#pragma mark 三方配置
- (void)vendorUtilsSetupWithOptions: (NSDictionary *)options application: (UIApplication *)application {
    
}

@end
